package com.example.demo.concurrent.Singleton;

/**
 * 双重校验锁(DCL) -> 创建单例  -- 单例模式-懒汉式
 */
public class Singleton4 {

    private Singleton4() {
        System.out.println("Singleton4()");
    }
    //为啥要加volatile ?

    private static volatile Singleton4 INSTANCE;

    public static Singleton4 getInstance() {
        if (INSTANCE == null) {
            synchronized (Singleton4.class) {
                if (INSTANCE == null) {
                    INSTANCE = new Singleton4();
                }
            }
        }
        return INSTANCE;
    }
}